<?php
require_once('MySqlConnection.php');
require_once('SiteStatistics.php');

class SiteStatisticsDAL{

	private $_connection;
	
	//SINGLETON PATTERN
    private static $_instance = null;
	public static function Instance(){
		if (self::$_instance == null) self::$_instance = new SiteStatisticsDAL();
		return self::$_instance;
	}
	//SINGLETON PATTERN END
	
	private function __construct(){
		include_once('./include/db.inc');
		$this->_connection = MySqlConnection::Instance($server,$user,$password,$database);
	}
	
	public function getSiteStatistics(){
		$this->_connection->Open();
		$sqlString = "select COUNT(*) as numberOfUsers from user u";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		
		$numberOfUsers = $resultData[0]->numberOfUsers;
		
		$sqlString = "select COUNT(*) as numberOfMaleUsers from user u JOIN salutation s ON u.salutation_id = s.id WHERE s.sex = 0";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		
		$numberOfMaleUsers = $resultData[0]->numberOfMaleUsers;
		
		$sqlString = "select COUNT(*) as numberOfFemaleUsers from user u JOIN salutation s ON u.salutation_id = s.id WHERE s.sex = 1";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		
		$numberOfFemaleUsers = $resultData[0]->numberOfFemaleUsers;
		
		$resultArray[] = new SiteStatistics($numberOfUsers, $numberOfMaleUsers, $numberOfFemaleUsers);
		
		return $resultArray;
		
		//return $this->Mapping($resultData, 'sitestatistics');
	}
	
	//MAPPING	
	private function Mapping($queryData,$resultType){
		foreach($queryData as $data){
			$resultArray[] = $this->Map($data,$resultType);
		}
		return $resultArray;
	}
	
	private function Map($data,$resultType){
		$object = new SiteStatistics($data->id,$data->name);
		return $object;
	}
	//MAPPING END
	
}

